import orc from "../../../orc";

/*
 * @Author: orc
 * @we_chat: orc_develop
 * @Date: 2019-01-24 11:51:38
 * @LastEditors: orc
 * @LastEditTime: 2019-03-22 14:21:58
 * @Description: 游戏授权按钮
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {   
    
    onLoad () {
       
    }

    async start () {
       
    } 

    onEnable(){
        console.log('orc.local_data.user_name:',orc.local_data.user_name);
        if(orc.local_data.user_name == null || orc.local_data.user_name == undefined || orc.local_data.user_name == '')
        {            
            console.log('用户未授权,生成授权按钮');
            this.btn_enter_game();
        }
        else
        {
            console.log('用户已授权，不生成授权按钮');
        }
    }
    

    btn_enter_game(){
        if(orc.game_config.Platform == 'wx')
        {
            console.log('wx_button',this.node.width,this.node.height);
            let btnSize = cc.size(this.node.width+10,this.node.height+10);            
            let frameSize = cc.view.getFrameSize();           
            let winSize = cc.winSize;   
            console.log('winSize:',winSize)
            let left = (winSize.width*0.5+this.node.x-btnSize.width*0.5)/winSize.width*frameSize.width;
            let top = (winSize.height*0.5-this.node.y-btnSize.height*0.5)/winSize.height*frameSize.height;
            let width = btnSize.width/winSize.width*frameSize.width;
            let height = btnSize.height/winSize.height*frameSize.height;
           
            let button = window["wx"].createUserInfoButton({
                type: 'text',
                text: ' ',
                style: {
                    left: left,
                    top: top,
                    width: width,
                    height: height,
                    lineHeight: 0,
                    backgroundColor: '',
                    color: '#ffffff',
                    textAlign: 'center',
                    fontSize: 16,
                    borderRadius: 4
                }
            });
            button.onTap(async (res) => {                
                if(res.errMsg == "getUserInfo:ok")
                {
                    console.log("用户授权成功",res);
                    button.hide(); 
                    orc.local_data.user_name = res.userInfo.nickName;
                    orc.local_data.user_image = res.userInfo.avatarUrl;                                        
                    orc.local_data.save_local_data();  
                    //告诉服务器存储
                    let res_set_userinfo = await orc.cloud.set_userinfo(res.userInfo.nickName,res.userInfo.avatarUrl);
                    if(res_set_userinfo == 'error')
                    {
                        console.log('cloud set_userinfo error');
                        return;
                    }
                    else
                    {
                        console.log('cloud set_userinfo success');
                        this.node.active = false;
                    }
                }
                else
                {
                    console.log("用户拒绝了授权");                    
                }               
            })    
        }
    }

   
    // update (dt) {}
}
